package com.lzp.controller;

import com.lzp.service.StuService;
import com.lzp.service.TransactionalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Web访问层
 *
 * @author lzp
 * @date 2020/6/24
 */
@ApiIgnore
@RestController // @RestController是@ResponseBody和@Controller的组合注解。
public class HelloController {

    private static final Logger logger =
            LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private StuService stuService;

    @GetMapping("/hello")
    public Object hello() {
        logger.info("info log");
        logger.warn("warn log");
        logger.error("error log");

        return "hello world";
    }

    @GetMapping("/getStu")
    public Object getStu(int id) {
        return stuService.getStuInfo(id);
    }

    @PostMapping("saveStu")
    public Object saveStu() {
        stuService.saveStu();
        return "OK";
    }

    @PostMapping("/updateStu")
    public Object updateStu(int id) {
        stuService.updateStu(id);
        return "OK";
    }

    @PostMapping("/deleteStu")
    public Object deleteStu(int id) {
        stuService.deleteStu(id);
        return "OK";
    }

    @Autowired
    TransactionalService transactionalService;

    /**
     * 测试事务传播性
     *
     * @return
     */
    @GetMapping("/test")
    public Object test() {
        transactionalService.testPropagation();
        return "OK";
    }

    /**
     * session理解
     *
     * @param request
     * @return
     */
    @GetMapping("/setSession")
    public Object setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("userInfo", "new User");
        session.setMaxInactiveInterval(3600);
        session.getAttribute("userInfo");
        // session.removeAttribute("userInfo");
        return "OK";
    }

    @GetMapping("/testIntercept")
    public Object testIntercept() {
        return "hello testIntercept";
    }
}
